package com.haoze.nurseapp.schedule;

import com.haoze.nurseapp.dao.StaticCenterMapper;
import com.haoze.nurseapp.model.StaticCenterEntity;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConditionalOnProperty(prefix = "schedule", name = "copyData", havingValue = "true")
public class StaticCenterSchedule {

    @Resource
    StaticCenterMapper staticCenterMapper;

    /**
     * 每分钟增量同步静配中心配置数据
     */
    @Scheduled(fixedDelay = 1000*5*60)
    @Transactional(rollbackFor = Exception.class)
    public void queryStaticCenter() {
        System.out.println("StaticCenterSchedule did at "+new Date().getTime());
        copyIntoStaticCenter(staticCenterMapper.queryPatientDrugMaster());
    }

    private void copyIntoStaticCenter(List<Map<String, String>> list) {
        list.parallelStream().forEach(rs -> {
            String inpatNum = rs.get("PATIENT_NO") + "-1";
            String orderNo = rs.get("ORDER_NO");
            String batch = rs.get("BATCH");
            String doctorCode = rs.get("DOCTOR_CODE");
            String barcode = rs.get("BARCODE");
            String useDate = rs.get("USE_DATE");
            String useTime = rs.get("USE_TIME");
            String frequency = rs.get("FREQUENCY");
            String userId = staticCenterMapper.queryUserId(doctorCode);
            StaticCenterEntity staticCenterEntity = new StaticCenterEntity();
            staticCenterEntity.setBarCode(barcode);
            staticCenterEntity.setDataVersion(1);
            staticCenterEntity.setGmtCreate(new Date());
            staticCenterEntity.setGmtModify(new Date());
            staticCenterEntity.setHospitalId("80D024E29BF40C9DE0537A5B14AC1620");
            staticCenterEntity.setBatch(batch);
            staticCenterEntity.setInpatNum(inpatNum);
            staticCenterEntity.setOrderNo(orderNo);
            staticCenterEntity.setUseDate(useDate);
            staticCenterEntity.setUseTime(useTime);
            staticCenterEntity.setFreq(frequency);
            staticCenterEntity.setDoctorId(userId);
            staticCenterEntity.setStatus("0");
            staticCenterMapper.insert(staticCenterEntity);
        });
    }
}
